package com.hqyj.Controller;
import com.hqyj.Pojo.Duty;
import com.hqyj.Pojo.User;
import com.hqyj.Service.DutyService;
import com.hqyj.Service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
//登录控制器
@Controller
//接收请求，调用业务，跳转页面
public class LoginController {
    //自动注入
    @Autowired
    LoginService LoginService;
    @Autowired
    DutyService dutyService;
    //表示已post提交方式提交给login.do 执行login方法
    @RequestMapping(value = "/login.do",method = RequestMethod.POST,produces={"text/plain;charset=utf-8"})
    public String login(String username,String password,HttpServletRequest request){
        List<User> list=LoginService.queryByusername(username);
        User userx=(User)request.getSession().getAttribute("user");
        //密码判断强制规定0号为正确用户，集合其他用户视为用户错误
        User users=null;
        try{
            users=list.get(0);
        }catch (Exception e){
            request.setAttribute("MSG","用户不存在");
            return  "index";
        }
        if(password.equals(users.getPassword())){//登录密码判断成功
            try {
                String name=userx.getUsername();
                if(name.equals(username)){
                    request.setAttribute("MSG","账户已登录!!");
                    return  "index";
                }
            }
            catch (Exception e){}
            long count=users.getQiandao();
            count++;
            users.setQiandao(count);
            LoginService.setqiandao(users);
            request.getSession().setAttribute("user",users);
            //返回姓名
            request.setAttribute("name",users.getName());
            //取出roleid
            int roleid=users.getRole_id();
            List<Duty> dutyList=dutyService.queryByRoleId(roleid);
            request.setAttribute("timer",count);
            request.setAttribute("dutyList",dutyList);
            //成功则页面跳转
            return "welcome";
        }
        //密码错误则停留
        request.setAttribute("MSG","密码错误");
        return  "index";
    }
    @RequestMapping(value = "/register.do",method = RequestMethod.POST,produces={"text/plain;charset=utf-8"})
    public  String register(User users, HttpServletRequest request){
        if(LoginService.queryByusername(users.getUsername()).size()!=0){
            request.setAttribute("MSG","用户名已被注册");
        }
        else{
            LoginService.InsertUser(users);
            request.setAttribute( "MSG","注册成功");
        }
        return "register";
    }
    @RequestMapping(value = "/forget.do",method = RequestMethod.POST,produces={"text/plain;charset=utf-8"})
    //@ResponseBody //不跳转，返回远内容
    public String forget(String username,String mail,HttpServletRequest request){
        List<User> list=LoginService.queryByusername(username);
        User users=null;
        try{
            users=list.get(0);
        }catch (Exception e){
            request.setAttribute("MSG","用户不存在");
            return  "forget";
        }
        if(mail.equals(users.getMail())){
            request.setAttribute("MSG","你的密码是:"+users.getPassword());
        }
        else{
            request.setAttribute("MSG","邮箱验证错误");
        }
        return  "forget";
    }
    @RequestMapping(value = "information.do",method = RequestMethod.GET)
    public String getlogininformation(HttpServletRequest request){
        User users=(User) request.getSession().getAttribute("user");
        request.setAttribute("user",users);
        request.setAttribute("addr","");
        return "PerInformation";
    }
    @RequestMapping(value = "updaeperson.do",method = RequestMethod.POST)
    public  String updaeperson(User user, HttpServletRequest request){
        User user2=(User) request.getSession().getAttribute("user");
        int userid=user2.getUser_id();
        user.setUser_id(userid);
        String rolename=user2.getRole_name();
        user.setRole_name(rolename);
        int roleid=user2.getUser_id();
        user.setRole_id(roleid);
        //用户名和密码没有修改
        if(user2.getUsername().equals(user.getUsername())&&user2.getPassword().equals(user.getPassword())){
//            if(user2.getName().equals(user.getName())&&user2.getSex().equals(user.getSex())&&user2.getMail().equals(user.getMail())&&user2.getAge().equals(user.getAge())&&user2.getAddress().equals(user.getAddress())){
//                request.setAttribute("MSG","你未修改任何数据");
//            }
//            else{
                dutyService.updateperson(user);
                request.setAttribute("MSG","修改成功!");
//            }
        }
        else{
            dutyService.updateperson(user);
            request.getSession().removeAttribute("user");//清除该用户的数据
            request.setAttribute("addr","index.jsp");
            return  "welcome";
        }
        request.setAttribute("user",user2);
        return  "PerInformation";
    }
    @RequestMapping(value = "exit.do",method = RequestMethod.POST)//退出界面操作
    public String exit(HttpServletRequest request){
        request.getSession().removeAttribute("user");//清除该用户的数据
        return "index";
    }
//    @RequestMapping(value = "timeout.do",method = RequestMethod.POST)//时间
//    public void timeout(HttpServletRequest request){
//        User user=(User) request.getSession().getAttribute("user");
//        user.setQiandao(0);
//        LoginService.setqiandao(user);
//    }
}
